//
//  AppDelegate.m
//  17-SandBox
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "AppDelegate.h"
#import "Book.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    // 沙盒(sandBox)
    // 1. Documents(文件文档, 用户主动数据存储)
    // 2. library (资源, 一般用来存放, 程序员要存储的一些数据)
    // (1)caches (缓存文件)
    // (2)preterences (用户信息, 和一些用户设置, NSUserDefaults)
    // 3. tmp (临时目录, 下载的临时文件, 一般放在这里)
    [[NSUserDefaults  standardUserDefaults] setBool:@"YES" forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    // 获取沙盒路径
    //快捷获取到对应目录的C函数
    // 根目录, 家目录
   NSLog(@"Home ------- %@",NSHomeDirectory());
    // 临时目录
    NSLog(@"%@",NSTemporaryDirectory());
    // C函数
    // 参数1:搜索文件夹路径
    // 常用 NSDocumentDirectory NSLibraryDirectory
    // 参数2:在用户作用与下搜索
    // 参数3:YES or NO, YES代表绝对路径(基本上都用绝对), NO代表相对路径(~)
  NSArray *pathArray =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", [pathArray firstObject]);
    // NSBundle 对应的.app文件包
    NSLog(@"%@", [NSBundle mainBundle]);
    // 简单的文件读写 Input Output
    // 1.将文字, 写入本地
    NSString *str = @"HELLO, I/O file";
    // 一般拼接路径时, 使用
    NSString *path = [[pathArray firstObject]stringByAppendingString:@"hello.text"];
    NSError *error = nil;
    // [[pathArray firstObject] stringByAppendingString:@"/hello.text"];
   BOOL isSuccess = [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (error) {
        NSLog(@"存储失败");
    } else {
        NSLog(@"存储成功");
    }
    // 读取路径对应的文字
    // 字符串
    NSError *readError = nil;
   NSString *readStr = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&readError];
    NSLog(@"%@", readStr);
    // 将数字写入本地文件
    NSArray *array = @[@"含蓄", @"王黄", @"包铭礼", @"果宝"];
    NSString *arrPath = [[pathArray firstObject] stringByAppendingPathComponent:@"name.plist"];
    BOOL isArray =  [array writeToFile:arrPath atomically:YES];
    if (isArray) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    // 读取 将数组读入
    NSArray *nameArray =  [NSArray arrayWithContentsOfFile:arrPath];
    NSLog(@"%@", nameArray);
    // 字典
    NSDictionary *dic = @{@"1":@"2", @"3":@"4", @"5":@"6"};
    
    NSString *dicPath = [[pathArray firstObject]stringByAppendingPathComponent:@"dic.plist"];
    BOOL isDic = [dic writeToFile:dicPath atomically:YES];
    if (isDic) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    NSDictionary *nameDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];
    NSLog(@"%@", nameDic);
    // 将 Data类型写入本地
    UIImage *image = [UIImage imageNamed:@"guidePage_01"];
   NSData *imageData = UIImageJPEGRepresentation(image, 0.1);
    NSLog(@"%@", imageData);
    NSString *datepath = [[pathArray firstObject] stringByAppendingPathComponent:@"imageData"];
   BOOL isData = [imageData writeToFile:datepath atomically:YES];
    if (isData) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    // NSData文件 类型 读入
   NSData *data = [NSData dataWithContentsOfFile:datepath];
    // 将data类型转成Image类型
   UIImage *fileImage = [UIImage imageWithData:data];
   // UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)
   
#pragma mark - 复杂文件读写, 自定义类型
    // 归档/反归档 ,学名 序列化/反序列化
    // 1 归档, 将对象存储到本地
    Book *book = [Book new];
    book.bookName = @"iOS从入门到放弃";
    book.bookType = @"教育";
    book.bookPrice = @"0.1";
    book.bookAuthor = @"王黄";
    book.bookAddress = @"王煌黑科技出版社";
    NSString *bookPath = [[pathArray firstObject] stringByAppendingPathComponent:@"aasd"];
  BOOL isBook =  [NSKeyedArchiver archiveRootObject:book toFile:bookPath];
    if (isBook) {
        NSLog(@"写入成功");
    } else {
        NSLog(@"写入失败");
    }
    // 2 反归档
  Book *huang = [NSKeyedUnarchiver unarchiveObjectWithFile:bookPath];
    NSLog(@"%@", huang.bookName);
    // 如果对象想要实现归档和反归档
    // 1.对象对应的类需要签订<NSCoding>
    // 2.实现协议方法
    // 1> initWithCoder 反归档用
    // 2> encodeWithCoder 归档用
    // 3. 归档时, 使用NSKeyedArchiver
    // 4. 反归档时, 使用 NSKeyedUnarchiver

    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
